<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 15/12/18
 * Time: 上午11:58
 */
class Tools extends MY_Agent_Controller
{
    public function crawl()
    {

        $data = array(
            'title'     => '关键字抓取',
        );
        $this->load->view('agent/tools/crawl', $data);
    }


    public function crawl_save()
    {
        $source = (int)$this->input->get_post('source');
        $keyword = trim($this->input->get_post('keyword'));


        if ($source != CRAWL_SOURCE_SOGOU)
            show_error('该抓取源暂暂不支持!');


        if (empty ($keyword))
            show_error('关键字为空!');


        $url = 'http://weixin.sogou.com/weixin?type=1&query=%s&ie=utf8';
        $url = sprintf($url, $keyword);


        $this->load->model('Model_public', 'public');
        $this->load->model('crawl/Model_crawl', 'crawl');
        //$content = file_get_contents(FCPATH.'test.html');
        $content = $this->crawl->one($url);



        $this->load->model('analysis/Model_analysis_list', 'al');
        $data = $this->al->analysis($content);


        //保存公众号数据
        if (!empty($data['data']))
        {
            $tmp = $data['data'];


            $item = $this->format($tmp);
            if (!empty($item))
                $this->public->save($item);


            unset($data['data']);
        }



        //抓取分页的公众号
        if (!empty($data['page']))
        {
            foreach($data['page'] as $v)
            {
                $content = $this->crawl->one($v);
                $tmp = $this->al->analysis($content, false);
                $item = $this->format($tmp);
                if (empty($item))
                    continue;


                $this->public->save($item);
                sleep(1);
            }
        }


        $this->load->helper('url');
        redirect(url('admin').'Tools/crawl', 'refresh');
    }
}